package com.baosight.sczx.common.zm.cInterface.cf;

import java.util.HashMap;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.cInterface.Department;

/**
 * 罚款
 * <p>Title: Penalty.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-8-7 下午02:43:11  严洪浩  Create
 *</pre>
 */
public class Penalty {
	
	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	
	/**
	 * 隐患处理时 开具罚款
	 * <p>Function: Insert</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-7下午02:54:26 严洪浩
	 * </p>
	 *
	 */
	public static boolean Insert(EiInfo inInfo, String fdKeyId) {
		try {
			EiBlock eiBlock = inInfo.getBlock("result");
			
			if(null == UserSession.getInSessionProperty("orgCode")) {
				return false;
			}
			String fdSendnoteDepartmentId = UserSession.getInSessionProperty("orgCode").toString();//开单部门ID
			String fdSendnotePersonId = UserSession.getUserId() == null ? "" : UserSession.getUserId().toString();//开单人ID
			String fdSendnotePerson = UserSession.getInSessionProperty("UserCname") == null ? "" : UserSession.getInSessionProperty("UserCname").toString();//开单人
			
			String fdDepartmentName = eiBlock.getCellStr(0, "fdDepartmentName");
			
			if(null == fdDepartmentName || "".equals(fdDepartmentName)) {
				fdDepartmentName = Department.getInstance().getDepartmentNameById(eiBlock.getCellStr(0, "fdDepartmentId"));
			}
			
			Map<String, String> _argMap = new HashMap<String, String>();
			_argMap.put("fdKeyId", fdKeyId);
			_argMap.put("fdSendnoteDepartmentId", fdSendnoteDepartmentId);
			_argMap.put("fdSendnoteDepartment", Department.getInstance().getDepartmentNameById(fdSendnoteDepartmentId));
			_argMap.put("fdSendnoteDate", eiBlock.getCellStr(0, "fdSendnoteDate"));
			_argMap.put("fdClassify", eiBlock.getCellStr(0, "fdClassify"));
			_argMap.put("fdPenalty", eiBlock.getCellStr(0, "fdPenalty"));
			_argMap.put("fdDepartmentId", eiBlock.getCellStr(0, "fdDepartmentId"));
			_argMap.put("fdDepartmentName", fdDepartmentName);
			_argMap.put("fdPeopleId", eiBlock.getCellStr(0, "fdPeopleId"));
			_argMap.put("fdPeopleName", eiBlock.getCellStr(0, "fdPeopleName"));
			_argMap.put("fdReason", eiBlock.getCellStr(0, "fdReason"));
			_argMap.put("fdIsSubmit", "否");
			_argMap.put("fdStatus", "生效");
			_argMap.put("fdSendnotePersonId", fdSendnotePersonId);
			_argMap.put("fdSendnotePerson", fdSendnotePerson);
			_argMap.put("fdRemark", eiBlock.getCellStr(0, "fdRemark"));
			
			dao.insert("ZMCFPenalty.insert", _argMap);
			
			//发送消息
			String fdPenalty = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdPenalty");
			String people = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdPeopleName");
			String departmentId = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdDepartmentId");
			
			if(people.length()>60)
			{
				people = people.substring(0, 60) + "...";
			}
			
			MessageUtil messageUtil = new MessageUtil();
			messageUtil.setDao(dao);
			int result = messageUtil.insertIntoMessageCache("关于责任人"+people+"的" + fdPenalty + "元罚款单", "ZMCF20", departmentId, "", 1);
			
			if(result != 1)
			{
				System.out.println("消息发送失败:code:"+result);
			}
			//end
			
		} catch (Exception e) {
			System.out.println("罚款失败: "+ e.getMessage());
			return false;
		}
		return true;
	}
	
}
